package com.zkb.modules.server.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zkb.common.util.PageUtil;
import com.zkb.common.util.StringUtils;
import com.zkb.common.vo.AjaxResultVo;
import com.zkb.modules.base.BaseController;
import com.zkb.modules.job.util.RomzUtils;
import com.zkb.modules.server.model.TServerInfo;
import com.zkb.modules.server.service.TServerInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 新闻分类表 前端控制器
 */
@Controller
@RequestMapping("/serverCon")
public class ServerController extends BaseController {

	@Autowired
	private TServerInfoService tServerInfoService;

	/**
	 * 服务器管理
	 */
	@GetMapping("/server")
	public String serverPage() {
		return "server/list";
	}

	/**
	 * 类别列表数据
	 * 
	 * @param tServerInfo
	 * @param limit
	 * @param offset
	 * @return
	 */
	@RequiresPermissions("server:list")
	@PostMapping("/list")
	@ResponseBody
	public AjaxResultVo pageServers(TServerInfo tServerInfo, Integer limit, Integer offset) {
		try {
			Long userId=getUserId();
			if(userId.longValue()!=1){  //1为超管，可以看到所有的
				tServerInfo.setOwnerId(getUserId());
			}
			PageHelper.startPage(PageUtil.getPageNo(limit, offset), limit);
			List<TServerInfo> categoryList = tServerInfoService.pageServers(tServerInfo);
			PageInfo<TServerInfo> pageResult = new PageInfo<>(categoryList);
			AjaxResultVo resutlVo = AjaxResultVo.success();
			resutlVo.put("rows", pageResult.getList());
			resutlVo.put("total", pageResult.getTotal());
			return resutlVo;
		} catch (Exception e) {
			logger.error(String.format("服务器列表查询异常 ServerController.pageServers%s", e));
			return AjaxResultVo.error("查询失败");
		}
	}

	/**
	 * 跳转新增类别
	 * 
	 * @return
	 */
	@RequiresPermissions("server:add")
	@GetMapping("/add")
	public String add() {
		return "server/add";
	}


	/**
	 * 插入新增类别
	 *
	 * @param tServerInfo
	 * @return
	 */
	@RequiresPermissions("server:add")
	@PostMapping("/doAdd")
	@ResponseBody
	public AjaxResultVo addServer(TServerInfo tServerInfo) {
		try {
			if (StringUtils.isBlank(tServerInfo.getHost())) {
				return AjaxResultVo.error("host不能为空！");
			}
			if (StringUtils.isBlank(tServerInfo.getPort())) {
				return AjaxResultVo.error("端口不能为空！");
			}
			if (StringUtils.isBlank(tServerInfo.getUsername())) {
				return AjaxResultVo.error("用户名不能为空！");
			}
			if (StringUtils.isBlank(tServerInfo.getPassword())) {
				return AjaxResultVo.error("密码不能为空！");
			}
			if (StringUtils.isBlank(tServerInfo.getPasssalt())) {
				return AjaxResultVo.error("混淆密码不能为空！");
			}
			tServerInfo.setId(RomzUtils.getId());
			tServerInfo.setOwnerId(getUserId());
			if (tServerInfoService.save(tServerInfo)) {
				return AjaxResultVo.success("添加服务器成功");
			} else {
				return AjaxResultVo.error("添加服务器失败");
			}
		} catch (Exception e) {
			logger.error(String.format("服务器添加异常添加异常 ServerController.addServer%s", e));
			return AjaxResultVo.error("添加类别失败");
		}
	}

	/**
	 * 删除服务器
	 *
	 * @param id
	 * @return
	 */
	@RequiresPermissions("server:delete")
	@PostMapping("/delete")
	@ResponseBody
	public AjaxResultVo deleteServer(String id) {
		try {
			if (StringUtils.isEmpty(id)) {
				return AjaxResultVo.error("删除失败,未选择删除选项");
			}
			if (tServerInfoService.removeById(id)) {
				return AjaxResultVo.success("删除成功");
			}
			return AjaxResultVo.error("删除失败");
		} catch (Exception e) {
			logger.error(String.format("服务器删除异常 ServerController.deleteServer%s", e));
			return AjaxResultVo.error("删除失败");
		}
	}


	/**
	 * 编辑文章类别详情
	 *
	 * @param model
	 * @param id
	 * @return
	 */
	@RequiresPermissions("server:edit")
	@GetMapping("/edit")
	public String detail(Model model, String id) {
		TServerInfo tServerInfo = tServerInfoService.getById(id);
		model.addAttribute("tServerInfo", tServerInfo);
		return "server/detail";
	}

	/**
	 * 编辑服务器
	 *
	 * @param tServerInfo
	 * @return
	 */
	@RequiresPermissions("server:edit")
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResultVo editServer(TServerInfo tServerInfo) {
		try {
			if (tServerInfoService.updateById(tServerInfo)) {
				return AjaxResultVo.success("编辑成功");
			} else {
				return AjaxResultVo.error("编辑失败");
			}
		} catch (Exception e) {
			logger.error(String.format("编辑异常 ServerController.editServer%s", e));
			return AjaxResultVo.error("编辑类别失败");
		}
	}

	/**
	 * 编辑文章类别详情
	 *
	 * @param model
	 * @param id
	 * @return
	 */
	@RequiresPermissions("server:terminal")
	@GetMapping("/terminal")
	public String terminal(Model model, String id) {
		TServerInfo tServerInfo = tServerInfoService.getById(id);
		model.addAttribute("tServerInfo", tServerInfo);
		return "server/zkbshell";
	}
}
